﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bordspil.Models;

namespace Bordspil.Controllers
{
    public class UserController : Controller
    {
        private IAppRepository m_repo = null;

        public UserController()
        {
            m_repo = new AppRepository();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Games()
        {
            return View();
        }

        public ActionResult UserProfile()
        {
            var loggedInUser = (from x in m_repo.GetAllUsers()
                                where x.username == User.Identity.Name
                                select x).SingleOrDefault();
            
            return View(loggedInUser);
        }

        [HttpGet]
        public ActionResult EditProfile()
        {
            var user = (from x in m_repo.GetAllUsers()
                        where x.username == User.Identity.Name
                        select x).SingleOrDefault();

            return View(user);
        }

        [HttpPost]
        public ActionResult EditProfile(int id, FormCollection formData)
        {
            var oldUser = (from x in m_repo.GetAllUsers()
                           where x.ID == id
                           select x).SingleOrDefault();

            UpdateModel(oldUser);

            var crypto = new SimpleCrypto.PBKDF2();
            var encryptPW = crypto.Compute(oldUser.password);

            oldUser.password = encryptPW;
            oldUser.passwordSalt = crypto.Salt;

            m_repo.EditUser(oldUser);

            return RedirectToAction("Index", "Home");
        }

        public ActionResult Stats()
        {
            return View();
        }

        public ActionResult ProfilePoints()
        {
            return View();
        }
    }
}
